<?php

$db_obj = new db_obj();
$user_obj = new user_obj();
$filter_obj = new filter_obj();
$cash_obj = new cash_obj();
$lock_obj = new lock_obj();
$periods = array();

if (!empty($_REQUEST['locked_period']) && ($_REQUEST['locked_period'] == 1))
    error_obj::setError(new error_obj('locked_period', 'list.php', 'list.php'), 'Selected period was locked!');

if ($_SESSION['user']['user_type_id'] == USER_TYPE_ADMIN) {

    if (!empty($_REQUEST['action']) && ($_REQUEST['action'] == 'lock')) {
        $in_data = array();
        $selectedPeriodExplode = explode('/', $_REQUEST['selectedPeriod']);
        $in_data['month'] = $selectedPeriodExplode[0];
        $in_data['year'] = $selectedPeriodExplode[1];
        $in_data['type'] = 'cash';

        $is_locked = $lock_obj->getLocks($in_data);
        if (!error_obj::isError($is_locked) && !empty($is_locked)) {
            $result = $lock_obj->deleteLockRow($in_data['month'], $in_data['year'], $in_data['type']);
            if (error_obj::isError($result))
                error_obj::setError($result, UNEXCEPTED);
            else
                error_obj::setGood('Selected period was unlocked!');
        } else {
            $result = $lock_obj->addLock($in_data);
            if (error_obj::isError($result))
                error_obj::setError($result, UNEXCEPTED);
            else
                error_obj::setGood('Selected period was locked!');
        }
    }
}

for ($i = 2013; $i < date("Y") + 2; $i++) {
    for ($j = 1; $j < 13; $j++) {

        $temp = array(
            'period' => str_pad($j, 2, "0", STR_PAD_LEFT) . '/' . $i
        );

        $filter = array(
            'month' => $j,
            'year' => $i,
            'type' => 'cash'
        );
        $lock = $lock_obj->getLocks($filter);

        if (!error_obj::isError($lock) && !empty($lock))
            $temp['locked'] = 1;
        else
            $temp['locked'] = 0;

        $periods[] = $temp;
    }
}

$GLOBALS['template_data']['periods'] = $periods;
$GLOBALS['template_data']['prefill']['today'] = date("m/Y");

if (!isset($_SESSION['cash_pager'])) {
    $_SESSION['cash_pager'] = $GLOBALS['cash_pager'];
}

if (!isset($_SESSION['cash_order'])) {
    $_SESSION['cash_order'] = $GLOBALS['cash_order'];
}

if (isset($_SESSION['cash_fulltext']) && !empty($_SESSION['cash_fulltext']))
    $GLOBALS['template_data']['currentFulltext'] = $_SESSION['cash_fulltext'];

$filter = array();

$currentFilter = array();

if (isset($_SESSION['cash_filter'])) {
    $filter = $_SESSION['cash_filter'];
    $result = $filter_obj->updateFilter($_SESSION['user']['user_id'], 'cash_filter', $filter);
    if (error_obj::isError($result)) {
        error_obj::setError($result, UNEXCEPTED);
    }
    foreach ($_SESSION['cash_filter'] as $f => $v) {
        switch ($f) {
            case 'created':
                $currentFilter[$f] = base_obj::convertDateTo($v, 'dd/mm/yyyy H:i:s', 'datetime');
                break;
            case 'who_added':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'user':
                $currentFilter[$f] = $user_obj->getUserById($v, array('name', 'surname'));
                $currentFilter[$f] = $currentFilter[$f][0] . ' ' . $currentFilter[$f][1];
                break;
            case 'date':
                $currentFilter[$f] = base_obj::convertDateTo($v, 'dd/mm/yyyy', 'date');
                break;
            case 'amount':
                $currentFilter[$f] = $v;
                break;
            case 'type_of_move':
                $currentFilter[$f] = $cash_obj->getCashMoveTypeById($v);
                $currentFilter[$f] = $currentFilter[$f][1];
                break;
            case 's':
                if ($v == 1)
                    $currentFilter[$f] = 'Yes';
                else
                    $currentFilter[$f] = 'No';
                break;
            case 'period':
                $currentFilter[$f] = $v;
                break;
        }
    }
}

$GLOBALS['template_data']['currentFilter'] = $currentFilter;

if (!isset($_GET['page'])) {
    $_GET['page'] = 0;
}

if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_TECHNIC)
    $filter['user'] = $_SESSION['user']['user_id'];

$limit = ($_GET['page'] * $_SESSION['cash_pager']) . ' ,' . $_SESSION['cash_pager'];

if (isset($_SESSION['cash_fulltext']) && !empty($_SESSION['cash_fulltext'])) {
    $PAGER_COUNT = $cash_obj->getListOfCashesByFulltext($_SESSION['cash_fulltext'], $filter, $_SESSION['cash_order'], '', 'COUNT(*)');
    $cashes = $cash_obj->getListOfCashesByFulltext($_SESSION['cash_fulltext'], $filter, $_SESSION['cash_order'], $limit);
} else {
    $PAGER_COUNT = $cash_obj->getListOfCashes($filter, $_SESSION['cash_order'], '', 'COUNT(*)');
    $cashes = $cash_obj->getListOfCashes($filter, $_SESSION['cash_order'], $limit);
}
$PAGER_COUNT = $PAGER_COUNT[0][0];
$GLOBALS['template_data']['currentOrder'] = array(
    $_SESSION['cash_order']['col'],
    $_SESSION['cash_order']['order']
);
$GLOBALS['template_data']['total_count'] = $PAGER_COUNT;
$GLOBALS['template_data']['sum'] = 0;

foreach ($cashes as $key => $cash) {
    $cashes[$key]['created_format'] = base_obj::convertDateTo($cash['created'], 'dd/mm/yyyy H:i:s', 'datetime');
    $cashes[$key]['creator_info'] = $user_obj->getUserById($cash['who_added'], array('name', 'surname'));
    $cashes[$key]['user_info'] = $user_obj->getUserById($cash['user'], array('name', 'surname'));
    $cashes[$key]['d_format'] = base_obj::convertDateTo($cash['date'], 'dd/mm/yyyy', 'date');
    $cashes[$key]['locked'] = 0;
    if ($lock_obj->isEntryLocked($cashes[$key]['d_format']))
        $cashes[$key]['locked'] = 1;
    $cashes[$key]['type_format'] = $cash_obj->getCashMoveTypeById($cash['type_of_move']);
    $GLOBALS['template_data']['sum'] += $cash['amount'];
    $cashes[$key]['midsum'] = $GLOBALS['template_data']['sum'];
}


$GLOBALS['template_data']['cashes'] = $cashes;

$pager = $db_obj->pager($PAGER_COUNT, $_GET['page'], $_SESSION['cash_pager']);

$GLOBALS['template_data']['pager'] = $pager;

$sql = "SELECT SUM(amount) FROM " . $GLOBALS['tb_cash'] . " WHERE user='" . $_SESSION['user']['user_id'] . "'";
$GLOBALS['template_data']['myTotal'] = round($db_obj->getOne($sql), 2);
?>
